//
//  MKTRegisterManager.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/7/9.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MKTRegisterManager.h"
#import <MKUI/MKUI.h>
@implementation MKTRegisterOption
- (id)initWithKey:(id)key value:(id)value priority:(NSInteger)priority{
    if (self=[super init]) {
        self.key = key;
        self.value = value;
        self.priority = priority;
    }
    return self;
}
@end
@interface MKTRegisterManager(){
    @protected
    NSMutableDictionary *_registerOptions;//注册列表,key=category,value=@{key:@[MKTRegisterOption]}
}

@end
@implementation MKTRegisterManager
MKDEF_SINGLETON(MKTRegisterManager)
- (id)init{
    if (self=[super init]) {
        _registerOptions = [[NSMutableDictionary alloc] init];
    }
    return self;
}
- (void)registerOption:(MKTRegisterOption *)option{
    [self registerOption:option forCategory:nil];
}
- (void)registerOption:(MKTRegisterOption *)option forCategory:(NSString *)category{
    if(!category){
        category = @"";
    }
    NSMutableDictionary *optionsInCategory = _registerOptions[category];
    if(!optionsInCategory){
        optionsInCategory = [[NSMutableDictionary alloc] init];
        _registerOptions[category] = optionsInCategory;
    }
    id key = option.key;
    if(!key){
        key = @"";
    }
    NSMutableArray *options = optionsInCategory[key];
    if(!options){
        options = [[NSMutableArray alloc] init];
        [options addObject:option];
        optionsInCategory[key] = options;
    }else{
        if(![options containsObject:option]){
            [options addObject:option];
            //进行排序
            [options sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
                MKTRegisterOption *opt1 = (MKTRegisterOption *)obj1;
                MKTRegisterOption *opt2 = (MKTRegisterOption *)obj2;
                NSNumber *priority1 = @(opt1.priority);
                NSNumber *priority2 = @(opt2.priority);
                NSComparisonResult r = -1*[priority1 compare:priority2];
                return r;
            }];
        }
    }
}
- (void)unregisterOption:(MKTRegisterOption *)option{
    [self unregisterOption:option forCategory:nil];
}
- (void)unregisterOption:(MKTRegisterOption *)option forCategory:(NSString *)category{
    if(!category){
        category = @"";
    }
    NSMutableDictionary *optionsInCategory = _registerOptions[category];
    id key = option.key;
    if(!key){
        key = @"";
    }
    NSMutableArray *options = optionsInCategory[key];
    [options removeObject:option];
}
- (void)unregisterOptionsForKey:(id)key{
    [self unregisterOptionsForKey:key forCategory:nil];
}
- (void)unregisterOptionsForKey:(id)key forCategory:(NSString *)category{
    if(!category){
        category = @"";
    }
    NSMutableDictionary *optionsInCategory = _registerOptions[category];
    if(!key){
        key = @"";
    }
    [optionsInCategory removeObjectForKey:key];
}
- (id)valueRegistedForKey:(id)key{
    id value = [self valueRegistedForKey:key category:nil];
    return value;
}
- (id)valueRegistedForKey:(id)key category:(NSString *)category{
    MKTRegisterOption *option = [self optionRegistedForKey:key category:category];
    id value = option.value;
    return value;
}
- (MKTRegisterOption *)optionRegistedForKey:(id)key{
    MKTRegisterOption *option = [self optionRegistedForKey:key category:nil];
    return option;
}
- (MKTRegisterOption *)optionRegistedForKey:(id)key category:(NSString *)category{
    if(!category){
        category = @"";
    }
    NSMutableDictionary *optionsInCategory = _registerOptions[category];
    if(!key){
        key = @"";
    }
    NSArray *options = optionsInCategory[key];
    MKTRegisterOption *option = [options firstObject];
    return option;
}
- (NSDictionary *)optionsRegistedForCategory:(NSString *)category{
    if(!category){
        category = @"";
    }
    NSMutableDictionary *optionsInCategory = _registerOptions[category];
    return optionsInCategory;
}
@end
